<template>
  <div class="app-container">
    <el-form class="base-search-form" :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="70">
      <el-form-item label="渠道名称" prop="channelName">
        <el-input
          v-model="queryParams.channelName"
          placeholder="请输入渠道名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="客户端" prop="channelType">
        <el-select v-model="queryParams.channelType" placeholder="请选择" clearable>
          <el-option
            v-for="item in bus_channel_code"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="支付模式" prop="payType">
        <el-select v-model="queryParams.payType" placeholder="请选择" clearable>
          <el-option
            v-for="item in plat_order_pay_mode"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="AppID" prop="channelAppId">
        <el-input
          v-model="queryParams.channelAppId"
          placeholder="请输入AppID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="渠道状态" prop="enabled">
        <el-select v-model="queryParams.enabled" placeholder="请选择" clearable>
          <el-option
            v-for="item in bus_line_status"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="base-container mb-50px">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['channel:platchannelinfo:add']"
          >新增</el-button>
        </el-col>
        <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table
        border
        v-loading="loading"
        v-if="refreshTable"
        row-key="id"
        :data="channelList"
        class="fixed-scrollbar" 
        v-horizontal-scroll
        @selection-change="handleSelectionChange"
      >
        <!-- <el-table-column type="selection" width="55" align="center" /> -->
        <el-table-column label="渠道名称" align="center" prop="channelName" min-width="150" />
        <el-table-column label="客户端" align="center" prop="channelType" min-width="120">
          <template #default="scope">
            <dict-tag :options="bus_channel_code" :value="scope.row.channelType" @click="handleChannelTypeClick" />
          </template>
        </el-table-column>
        <el-table-column label="支付模式" align="center" prop="payType" min-width="130">
          <template #default="scope">
            <dict-tag :options="plat_order_pay_mode" :value="scope.row.payType" @click="handlePayTypeClick" />
          </template>
        </el-table-column>
        <el-table-column label="商户号" align="center" prop="channelMchId" min-width="150" />
        <el-table-column label="AppID" align="center" prop="channelAppId" min-width="150" />
        <el-table-column label="密钥状态" align="center" prop="hasKey" min-width="90">
          <template #default="scope">
            <el-tag type="warning" v-if="scope.row.hasKey==0">待配置</el-tag>
            <el-tag type="success" v-if="scope.row.hasKey==1">已配置</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="渠道状态" align="center" min-width="80">
          <template #default="scope">
            <el-switch
              v-model="scope.row.enabled"
              inline-prompt
              :active-value="1"
              :inactive-value="2"
              inactive-text="禁用"
              active-text="启用"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="更新时间" align="center" width="160" prop="updateTime" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150" fixed="right">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button :disabled="scope.row.enabled == 1" link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['channel:platchannelinfo:edit']"></el-button>
            </el-tooltip>
            <template v-if="scope.row.channelType != 1016">
              <el-tooltip content="配置" placement="top">
                <el-button :disabled="scope.row.enabled == 1" link type="primary" icon="Setting" @click="handleConfig(scope.row)" v-hasPermi="['channel:platchannelinfo:edit']"></el-button>
              </el-tooltip>
            </template>
            <el-tooltip content="密钥" placement="top">
              <el-button :disabled="scope.row.enabled == 1" link type="primary" icon="Key" @click="handleKeysConfig(scope.row)" v-hasPermi="['channel:platchannelinfo:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button :disabled="scope.row.enabled == 1" link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['channel:platchannelinfo:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
    
    <Dialog v-model="showDialog" :title="dialogTitle" :width="dialogWidth">
      <edit-channel
        v-if="dialogType === 1"
        ref="editChannel"
        @cancel="cancel"
        @confirm="confirm">
      </edit-channel>
      <channel-config
        v-if="dialogType === 2"
        ref="channelConfig"
        @cancel="cancel"
        @confirm="confirm">
      </channel-config>
      <channel-key
        v-if="dialogType === 3"
        ref="channelKey"
        @cancel="cancel"
        @confirm="confirm">
      </channel-key>
    </Dialog>
  </div>
</template>

<script setup name="Channelinfo">
import { listChannel, updateChannel, delChannel } from "@/api/channel/platchannelinfo"
import EditChannel from './edit.vue'
import ChannelConfig from './config.vue'
import ChannelKey from './key.vue'
import { nextTick } from "vue"

const editChannel = ref()
const channelConfig = ref()
const channelKey = ref()
const { proxy } = getCurrentInstance()
const { bus_channel_code, bus_line_status, plat_order_pay_mode } = proxy.useDict('bus_channel_code', 'bus_line_status', 'plat_order_pay_mode')

const channelList = ref([])
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const refreshTable = ref(true)
const showDialog = ref(false)
const dialogTitle = ref('')
const dialogType = ref(0)                 // 1修改  2配置  3密钥
const dialogWidth = ref('50%')

const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    channelName: null,
    enabled: null,
    channelType: null,
    payType: null,
    channelAppId: null
  }
})

const { queryParams } = toRefs(data)

const handleChannelTypeClick = (obj) => {
  queryParams.value.channelType = obj.value
  handleQuery()
}

const handlePayTypeClick = (obj) => {
  queryParams.value.payType = obj.value
  handleQuery()
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

// 密钥配置
function handleKeysConfig(row){
  showDialog.value = true
  dialogTitle.value = '密钥配置'
  dialogType.value = 3
  dialogWidth.value = '50%'
  nextTick(() => {
    channelKey.value.pageInit(row.id)
  })
}

// 配置
function handleConfig(row){
  showDialog.value = true
  dialogTitle.value = '渠道配置'
  dialogType.value = 2
  dialogWidth.value = '70%'
  nextTick(() => {
    channelConfig.value.pageInit(row.id)
  })
}

// 取消按钮
function cancel() {
  showDialog.value = false
  dialogTitle.value = ''
  dialogType.value = 0
  dialogWidth.value = '50%'
}

const confirm = () => {
  cancel()
  getList()
}

/** 新增按钮操作 */
function handleAdd() {
  showDialog.value = true
  dialogTitle.value = '新增渠道'
  dialogType.value = 1
  dialogWidth.value = '50%'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  const _id = row.id || ids.value
  showDialog.value = true
  dialogTitle.value = '修改渠道'
  dialogType.value = 1
  dialogWidth.value = '50%'
  nextTick(() => {
    editChannel.value.pageInit(_id)
  })
}

/** 状态修改 */
function handleStatusChange(row) {
  let text = row.enabled == 1 ? "启用" : "禁用"
  proxy.$modal.confirm('确认要"' + text + '"吗?').then(function () {
    const data = {
      enabled:row.enabled,id:row.id
    }
    return updateChannel(data)
  }).then(() => {
    proxy.$modal.msgSuccess(text + "成功")
  }).catch(function () {
    row.enabled = row.enabled === 1 ? 2 : 1
  })
}

/** 查询渠道列表 */
function getList() {
  loading.value = true
  listChannel(queryParams.value).then(response => {
    channelList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除该渠道？').then(function() {
    return delChannel(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

getList()
</script>
